<template>
  <div class="main">
    {{ imageListApp }}
    <div class="nav">
      <img :src="back" alt="" @click="handleBack" class="back"/>
      <div>医疗记录</div>
      <!--      <img :src="edit" alt="" @click="handleEdit" />-->
      <div class="edit" @click="handleComplete" v-if="!showLabel">完成</div>
      <div class="edit" @click="handleEdit" v-if="showLabel">编辑</div>
    </div>
    <div class="body" v-if="!showLabel">
      <div>今天</div>
      <div class="img-list">
        <div class="img-item-detail" v-for="(itemImage,indexImage) in imageListApp" :key="indexImage">
          <img :src="store.state.imgUrl+itemImage"/>
          <i v-if="!showLabel" class="el-icon-remove"
             style="color:#f29600;margin-left:2px;font-size:17px;position: absolute;right: -3px;top:-8px;"
             @click="handleDelApp(itemImage,indexImage)"></i>
        </div>
        <div class="img-item" @click="handleOpen">
          <img :src="addImage" class="bigImg"/>
        </div>
      </div>
    </div>
    <div class="body" v-for="(item,index) in imageList" :key="index">
      <div>{{ item.convertTime }}</div>
      <div class="img-list">
        <div class="img-item-detail" v-for="(itemImage,indexImage) in imageList[index].children" :key="indexImage">
          <img v-if="itemImage.picture!==null" :src="store.state.imgUrl+itemImage.picture"/>
          <i v-if="!showLabel && itemImage.picture!==null" class="el-icon-remove"
             style="color:#f29600;margin-left:2px;font-size:17px;    position: absolute;right: -3px;top:-8px;"
             @click="handleDel(itemImage.id)"></i>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {addSysMedicalRecond, deleteMedicalRecord} from "../../../api/my";
import store from '@/store'

export default {
  name: "medicalRecords",
  data() {
    return {
      store:store,
      back: require("@/assets/my/back.png"),
      edit: require('@/assets/my/edit1.png'),
      addImage: require("@/assets/addImage.png"),
      imageList: [],
      showLabel: false,
      info: '',
      imageListApp: [],
    }
  },
  mounted() {
    this.imageList = this.$route.query.imgList
    window["getInfoFromFlutter"] = (info) => {
      if (JSON.parse(info).action === 'upload') {
        this.getInfoFromFlutter(info);
      }
    };
  },
  methods: {
    getInfoFromFlutter(info) {
      let data = JSON.parse(info);
      if (data.param.type == "image") {
        if (data.param.images && data.param.images.length > 0) {
          this.imageListApp.push(data.param.images[0]);
        }
      }
    },
    handleBack() {
      this.$router.go(-1);
    },
    handleEdit() {
      this.showLabel = !this.showLabel
    },
    handleComplete() {
      //完成按钮
      // this.$toast('保存成功跳转到上一级')
      let data = {
        pictures: this.imageListApp
      }
      addSysMedicalRecond(data).then(res => {
        if (res.success) {
          this.$toast.success('医疗记录保存成功')
          this.$router.push({path: '/healthRecords'})
        } else {
          this.$toast.fail('医疗记录保存失败')
        }
      })
    },
    handleDelApp(item, index) {
      this.imageListApp.splice(index, 1)
    },
    handleDel(id) {
      let data = {
        id: id
      }
      console.log(data)
      deleteMedicalRecord(data).then(res => {
        if (res.success) {
          this.$toast.success('删除成功')
          this.imageList.forEach((item, index) => {
            item.children.forEach((itemDetail, itemIndex) => {
              if (itemDetail.id == id) {
                item.children.splice(itemIndex, 1)
              }
            })
          })
        }
      })
    },
    handleOpen() {
      let message = {
        action: 'upload',
        param: {
          type: "image",            //必填
        }
      }
      getInfoFromVue.postMessage(JSON.stringify(message))
    },
  }
}
</script>

<style scoped lang="less">
.main {
  height: 100vh;
  padding: 0px 15px;
  overflow-y: scroll;

  .nav {
    height: 44px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 500;
    font-size: 20px;

    .back {
      width: 10px;
      height: 16px;
      position: absolute;
      left: 15px;
    }

    .edit {
      //width: 21.39px;
      height: 22px;
      position: absolute;
      right: 15px;
    }
  }

  .body {
    padding: 40px 0px;

    .img-list {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-wrap: wrap;

      .img-item-detail {
        width: calc((100% - 18px) / 3);
        margin-right: 9px;
        height: 122px;
        opacity: 1;
        border-radius: 10px;
        margin-top: 18px;
        position: relative;

        > img {
          width: 100%;
          height: 122px;
          background-size: 100%;
          background-repeat: no-repeat;
        }
      }

      .img-item-detail:nth-of-type(3n + 0) {
        margin-right: 0px;
      }

      .img-item {
        width: 100px;
        margin-right: 9px;
        height: 122px;
        opacity: 1;
        border-radius: 10px;
        background: rgba(229, 229, 229, 1);
        margin-top: 18px;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;

        > img {
          width: 25px;
          height: 62.78px;
          opacity: 1;
        }

        > div {
          position: absolute;
          right: 5px;
          bottom: 5px;
          font-size: 16px;
          font-weight: 500;
          letter-spacing: 0px;
          line-height: 23.17px;
          color: rgba(166, 166, 166, 1);
        }
      }
    }
  }
}
</style>
